package com.sz.sjtb.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sz.sjtb.domain.User;
import com.sz.sjtb.service.UserService;
import com.sz.sjtb.util.ConvertUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    private Logger logger = Logger.getLogger(getClass());
    @Autowired
    @Qualifier("primaryJdbcTemplate")
    protected JdbcTemplate jdbcTemplate1;
    @Autowired
    @Qualifier("secondaryJdbcTemplate")
    protected JdbcTemplate jdbcTemplate2;

    @Override
    public List<User> getAllUsers() {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT * FROM USER1");
        Object[] params2 = new Object[] {};
       /*List list = null;
        try {
            list = jdbcTemplate1.queryForList(sql.toString(), params2);
            logger.info("users="+ JSONObject.toJSONString(list));
        } catch (Exception e) {
            e.printStackTrace();
        }
        list.stream().forEach(vo ->{
            User u = (User)vo;
            logger.info(u.getAge());
        });*/
        List list = null;
        List<User> users = null;
        try {
            list = jdbcTemplate2.queryForList(sql.toString(), params2);
            users = ConvertUtil.convertMapListToBeanList(list,User.class);
            logger.info("users="+ JSONObject.toJSONString(list));
            String sql1 = "insert into user1 values(?,?,?)";
            int[] insertCount = this.BatchInsertUser(users,sql1);
            logger.info("insertCount="+ JSONObject.toJSONString(insertCount));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return users;
    }

    @Override
    public Integer getUserCount() {
        Integer source1 = jdbcTemplate1.queryForObject("select	count(1) from USER", Integer.class);
        logger.info(source1);
        Integer source2 = jdbcTemplate2.queryForObject("select	count(1) from USER", Integer.class);
        logger.info(source2);
        return source1+source2;
    }
    public int[] BatchInsertUser(List<User> list, String v_sql) {

        System.out.println("list.size()=" + list.size());
        return jdbcTemplate1.batchUpdate(v_sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setInt(1, list.get(i).getId());
                ps.setString(2, list.get(i).getName());
                ps.setInt(3, list.get(i).getAge());
            }
            @Override
            public int getBatchSize() {
                return list.size();
            }
        });

    }
}
